package co.yixiang.yshop.module.product.dal.mysql.campaigninfo;

import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.mybatis.core.mapper.BaseMapperX;
import co.yixiang.yshop.framework.mybatis.core.query.LambdaQueryWrapperX;
import co.yixiang.yshop.module.product.controller.admin.campaigninfo.param.CampaignJudgmentParam;
import co.yixiang.yshop.module.product.controller.admin.campaigninfo.vo.CampaignInfoExportReqVO;
import co.yixiang.yshop.module.product.controller.admin.campaigninfo.vo.CampaignInfoPageReqVO;
import co.yixiang.yshop.module.product.controller.admin.campaigninfo.vo.CampaignOrderVo;
import co.yixiang.yshop.module.product.controller.admin.campaigninfo.vo.CanvasCampaignPageReqVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignNumberJudgeVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductReqVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignProductRespVO;
import co.yixiang.yshop.module.product.controller.app.campaigninfo.vo.CampaignSkuVO;
import co.yixiang.yshop.module.product.dal.dataobject.campaigninfo.CampaignInfoDO;
import co.yixiang.yshop.module.product.enums.campaign.CampaignStateEnum;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.Arrays;
import java.util.List;

/**
 * 活动信息 Mapper
 *
 * @author moxiangrong
 */
@Mapper
public interface CampaignInfoMapper extends BaseMapperX<CampaignInfoDO> {

    List<Long> checkRepeat(CampaignJudgmentParam param);

    default PageResult<CampaignInfoDO> selectPage(CampaignInfoPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CampaignInfoDO>()
                .likeIfPresent(CampaignInfoDO::getName, reqVO.getName())
                .eqIfPresent(CampaignInfoDO::getType, reqVO.getType())
                .eqIfPresent(CampaignInfoDO::getState, reqVO.getState())
                .betweenIfPresent(CampaignInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CampaignInfoDO::getId));
    }

    default PageResult<CampaignInfoDO> getCanvasCampaignPage(CanvasCampaignPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<CampaignInfoDO>()
                .likeIfPresent(CampaignInfoDO::getName, reqVO.getName())
                .eqIfPresent(CampaignInfoDO::getType, reqVO.getType())
                .in(CollectionUtils.isNotEmpty(reqVO.getIds()), CampaignInfoDO::getId, reqVO.getIds())
                .in(CampaignInfoDO::getState, Arrays.asList(CampaignStateEnum.PREHEAT.getValue(),
                        CampaignStateEnum.IN_PROGRESS.getValue()))
                .orderByDesc(CampaignInfoDO::getId));
    }

    default List<CampaignInfoDO> selectList(CampaignInfoExportReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<CampaignInfoDO>()
                .likeIfPresent(CampaignInfoDO::getName, reqVO.getName())
                .eqIfPresent(CampaignInfoDO::getType, reqVO.getType())
                .eqIfPresent(CampaignInfoDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(CampaignInfoDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(CampaignInfoDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(CampaignInfoDO::getIfLimit, reqVO.getIfLimit())
                .eqIfPresent(CampaignInfoDO::getLimitNumber, reqVO.getLimitNumber())
                .eqIfPresent(CampaignInfoDO::getIfEnable, reqVO.getIfEnable())
                .eqIfPresent(CampaignInfoDO::getEnableTime, reqVO.getEnableTime())
                .eqIfPresent(CampaignInfoDO::getState, reqVO.getState())
                .eqIfPresent(CampaignInfoDO::getPerson, reqVO.getPerson())
                .eqIfPresent(CampaignInfoDO::getEffectiveTime, reqVO.getEffectiveTime())
                .betweenIfPresent(CampaignInfoDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(CampaignInfoDO::getId));
    }

    List<CampaignOrderVo> getCampaignInfoList(@Param("campaignIds") List<Long> campaignIds);

    List<CampaignProductRespVO> getCampaignProductPage(Page page, @Param("reqVO") CampaignProductReqVO reqVO);

    List<CampaignSkuVO> getCampaignsByProductAttrUniques(@Param("productAttrUniques") List<String> productAttrUniques);

    Integer numberJudge(@Param("vo") CampaignNumberJudgeVO vo);

    Integer stockJudge(@Param("vo") CampaignNumberJudgeVO vo);

    void deStock(@Param("productAttrUnique") String productAttrUnique, @Param("number") Integer number);

}
